package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.FormatTool;
import com.j283_5.wisdom_park.mapper.CarInformationMapper;
import com.j283_5.wisdom_park.mapper.CarRecordMapper;
import com.j283_5.wisdom_park.mapper.PersonMapper;
import com.j283_5.wisdom_park.mapper.VisitorsMapper;
import com.j283_5.wisdom_park.pojo.*;
import com.j283_5.wisdom_park.service.CarRecordService;
import com.j283_5.wisdom_park.vo.car_management.CarRecordVo;
import com.j283_5.wisdom_park.vo.car_management.SelectCarRecordVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class CarRecordServiceImpl extends ServiceImpl<CarRecordMapper, CarRecord> implements CarRecordService {
    @Resource
    private CarRecordMapper carRecordMapper;
    @Resource
    private CarInformationMapper carInformationMapper;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private VisitorsMapper visitorsMapper;

    /**
     * 车辆进出信息查询
     * @param page
     * @param selectCarRecordVo
     * @return
     */
    @Override
    public ResultData carRecordVoList(Page<CarRecord> page, SelectCarRecordVo selectCarRecordVo) {
        QueryWrapper<CarRecord> carRecordQueryWrapper = new QueryWrapper<>();
        carRecordQueryWrapper.eq("crec_state",0);


        carRecordQueryWrapper.orderByDesc("crea_time");
        if(selectCarRecordVo.getCreaTime() != null && selectCarRecordVo.getCreaTime().trim().length() > 0){
            carRecordQueryWrapper.like("crea_time",selectCarRecordVo.getCreaTime());


        }
        if(selectCarRecordVo.getCinfMark() != null && selectCarRecordVo.getCinfMark().trim().length() > 0){
            QueryWrapper<CarInformation> carInformationQueryWrapper = new QueryWrapper<>();
            carInformationQueryWrapper.eq("cinf_mark",selectCarRecordVo.getCinfMark());
            List<CarInformation> carInformationList = carInformationMapper.selectList(carInformationQueryWrapper);
            if(carInformationList.size() > 0){
                ArrayList<Integer> cinfIdList = new ArrayList<>();
                for(CarInformation carInformation : carInformationList){
                    cinfIdList.add(carInformation.getCinfId());
                }
                carRecordQueryWrapper.in("cinf_id",cinfIdList);
            }
        }
        CarRecordVo carRecordVo = null;
        Page<CarRecord> carRecordPage = carRecordMapper.selectPage(page, carRecordQueryWrapper);
        List<CarRecord> carRecordList = carRecordPage.getRecords();
        if(carRecordList .size()>0){
            ArrayList<CarRecordVo> carRecordVoList = new ArrayList<>();
            for(CarRecord carRecord : carRecordList){
                carRecordVo = new CarRecordVo(carRecord.getCinfId(),carRecord.getCrecId(),
                        (carRecord.getCreaIo()==0?"入场":"出场"),carRecord.getCreaTime());
                CarInformation carInformation = carInformationMapper.selectById(carRecord.getCinfId());
                carRecordVo.setCinfMark(carInformation.getCinfMark());
                carRecordVo.setCinfType(carInformation.getCinfType());
                carRecordVo.setCinfClass(carInformation.getCinfClass());
                if("visitors".equals(carInformation.getOwnerType())){
                    Visitors visitors = visitorsMapper.selectById(carInformation.getOwnerId());
                    if(visitors != null){
                        carRecordVo.setOwnerName(visitors.getVisName());
                        carRecordVo.setOwnerPhone(visitors.getVisPhone());
                    }
                }else if("person".equals(carInformation.getOwnerType())){
                    Person person = personMapper.selectById(carInformation.getOwnerId());
                    if(person != null){
                        carRecordVo.setOwnerName(person.getPersonName());
                        carRecordVo.setOwnerPhone(person.getPersonPhone());
                    }
                }
                carRecordVoList.add(carRecordVo);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("data",carRecordVoList);
            map.put("count",carRecordPage.getTotal());
            return ResultData.ok().data(map);

        }
        return ResultData.error().data("没有查询到相关数据");
    }

    @Override
    public ResultData deleteRecordVo(ArrayList<Integer> crecIdList) {
        UpdateWrapper<CarRecord> carRecordUpdateWrapper = new UpdateWrapper<>();
        carRecordUpdateWrapper.set("crec_state",1);
        carRecordUpdateWrapper.in("crec_id",crecIdList);
        int i = carRecordMapper.update(null, carRecordUpdateWrapper);
        if(i>0){
            return ResultData.ok().message("删除成功！");
        }else{
            return ResultData.error().message("删除失败！");
        }
    }


}
